* Figure 1 - Redistribution and the probability that the best performer lost (1 - pb)
#delimit;
use "${data}/spectator.dta", replace;
drop if condition == "EP";
drop if info      == "yes";

gen n = 1;
collapse (mean) shr_red (sum) n (min) min_pi=pi (max) max_pi=pi (semean) semean = shr_red, by(condition pi_bin); gen mid_bin = (max_pi+min_pi)/2;

replace mid_bin = 100 - mid_bin;
replace mid_bin = mid_bin+0.35 if condition == "EA";
replace mid_bin = mid_bin-0.35 if condition == "CF";

#delimit;
cap drop x* y*; gen x1 = 25; gen x2 = 25; gen y1 = .45; gen y2 = .49;
cap drop w* z*; gen w1 = 25; gen w2 = 25; gen z1 = .05; gen z2 = .01;

cap drop a* b*; gen a1 = 4 ; gen a2 = 1 ; gen b1 = .45  ; gen b2 = .45;
cap drop c* d*; gen c1 = 45; gen c2 = 49; gen d1 = .045 ; gen d2 = .045;

#delimit;
gen up = shr_red + 1.96*semean;
gen lo = shr_red - 1.96*semean;

local my_green "0 158 115";
local my_blue  "0 114 178"; 

#delimit;
local emdash = ustrunescape("\u2013");
twoway connected shr_red mid_bin if condition == "CF", lcolor("black")             mcolor("black")    msize(small) msym(D)
	|| connected shr_red mid_bin if condition != "CF", lcolor("gray") lp(solid)  mcolor("gray")   msize(small) msym(O)
	|| rcap      up lo   mid_bin if condition == "CF", lcolor("black")  lpattern(dash) lwidth(vthin)
	|| rcap      up lo   mid_bin if condition != "CF", lcolor("gray") lpattern(dash) lwidth(vthin)
	|| pcarrow y1 x1 y2 x2, lcolor(gs12)   mcolor(gs12)   
    || pcarrow z1 w1 z2 w2, lcolor(gs12)   mcolor(gs12)   
	|| pcarrow b1 a1 b2 a2, color(gs8) 
    || pcarrow d1 c1 d2 c2, color(gs8)
	   xtitle("Probability that the best performer lost (1 `emdash' {&pi})") xlabel(0(10)50, format(%4.0fc)) xscale(titlegap(2)) 
	   ytitle("Fraction of winner's earnings redistributed") ylabel(0(.1).5, format(%4.2fc)  angle(0) gmin gmax)
       yline(0.5, lpattern(dash) lcolor(gs12)) text(.43 25 "Earnings equalization", color(gs8))
       yline(0.0, lpattern(dash) lcolor(gs12)) text(.07 25 "Laissez-faire", color(gs8))
       xline(50,  lpattern(dash) lcolor(gs8))  text(.45 9 "Performance" "determines" "winner", color(gs8))
       xline(0, lpattern(dash) lcolor(gs8))    text(.05 40 "Chance" "determines" "winner", color(gs8))
       legend(order(1 "Lucky Outcomes" 2 "Lucky Opportunities") row(1))
	   graphregion(color(white)) bgcolor(white);
  	   graph export "${res}/fig1.pdf", replace as(pdf);



* Figure 2 - Relationship between the redistribution and the features of the luck environment observed by spectators;
* Figure 6 - Relationship between the redistribution and the features observed by spectators (information intervention);

* Panel A. Average redistribution vs. Coin-flip probability;
#delimit;
use "${data}\spectator.dta", replace;
keep if condition == "CF";

forvalues p = 1(1)7 {; gen pi_`p' = pi^`p'; };

gen shr_red_res =.;
reg shr_red pi if info == "no" ; predict xb; gen temp = shr_red - xb; replace shr_red_res = temp if info == "no"  ; qui sum shr_red if info == "no"  , meanonly; replace shr_red_res = shr_red_res + r(mean) if info == "no"  ; drop temp xb;
reg shr_red pi if info == "yes"; predict xb; gen temp = shr_red - xb; replace shr_red_res = temp if info == "yes" ; qui sum shr_red if info == "yes" , meanonly; replace shr_red_res = shr_red_res + r(mean) if info == "yes" ; drop temp xb;

xtile quant = q , nq(15);
bys quant: egen xgrid = mean(q);

lpoly shr_red     q if info == "no"   , gen(temp) nograph degree(1) at(xgrid) bw(30) kernel(triangle); by quant: egen lpoly_noinfo     =mean(temp); drop temp;
lpoly shr_red     q if info == "yes"  , gen(temp) nograph degree(1) at(xgrid) bw(30) kernel(triangle); by quant: egen lpoly_info       =mean(temp); drop temp;
lpoly shr_red_res q if info == "no"   , gen(temp) nograph degree(1) at(xgrid) bw(30) kernel(triangle); by quant: egen lpoly_res_noinfo =mean(temp); drop temp;
lpoly shr_red_res q if info == "yes"  , gen(temp) nograph degree(1) at(xgrid) bw(30) kernel(triangle); by quant: egen lpoly_res_info   =mean(temp); drop temp;

gen lpoly     = .; replace lpoly     = lpoly_noinfo     if info == "no"  ; replace lpoly     = lpoly_info       if info == "yes" ;
gen lpoly_res = .; replace lpoly_res = lpoly_res_noinfo if info == "no"  ; replace lpoly_res = lpoly_res_info   if info == "yes" ;

gen n = 1;

collapse (mean) shr_red* lpoly lpoly_res q (sd) sd = shr_red (semean) semean = shr_red semean_res = shr_red_res (sum) n, by(info quant);

gen up = shr_red + 1.96*semean;
gen lo = shr_red - 1.96*semean;

gen up_res = shr_red_res + 1.96*semean_res;
gen lo_res = shr_red_res - 1.96*semean_res;

#delimit;
twoway scatter shr_red        q if info == "no" , lcolor(black)      mcolor(black)   msym(O) 
 	|| rcap      up lo        q if info == "no" , lcolor(gs8)   lpattern(dash) lwidth(vthin)
    || line      lpoly        q if info == "no" , lcolor(black)
	   xtitle("Probability that a coin flip determined the winner")   xlabel(0(20)100, format(%4.0fc)) xscale(titlegap(2))
	   ytitle("Average fraction of earnings redistributed")  ylabel(0(.1).5, format(%4.2fc)  angle(0) gmin gmax)
	   legend(off) 
	   graphregion(color(white)) bgcolor(white);
	   graph export "${res}\fig2a.pdf", replace as(pdf);
	   
	   	   
#delimit;
twoway scatter shr_red         q if info == "no"  , lcolor(black)        mcolor(black)   msym(O) 
    || scatter shr_red         q if info == "yes" , lcolor(gs6)          mcolor(gs6)     msym(T) 
    || scatter shr_red_res     q if info == "yes" , lcolor(gs12)         mcolor(gs12)    msym(S) 
 	|| rcap      up lo         q if info == "no"  , lcolor(black%75)     lpattern(dash)  lwidth(vthin)
 	|| rcap      up lo         q if info == "yes" , lcolor(gs6%75)       lpattern(dash)  lwidth(vthin)
 	|| rcap      up_res	lo_res q if info == "yes" , lcolor(gs12%75)      lpattern(dash)  lwidth(vthin)
    || line      lpoly         q if info == "no"  , lcolor(black)
    || line      lpoly         q if info == "yes" , lcolor(gs6)
    || line      lpoly_res     q if info == "yes" , lcolor(gs12)
	   xtitle("Probability that a coin flip determined the winner")   xlabel(0(20)100, format(%4.0fc)) xscale(titlegap(2))
	   ytitle("Average fraction of earnings redistributed")  ylabel(0(.1).5, format(%4.2fc)  angle(0) gmin gmax)
       legend(order(1 "Baseline Lucky Outcomes" 2 "Information Treatment" 3 "Information + Controlling for {&pi}") col(1) ring(0) position(5) bmargin(medium))
	   graphregion(color(white)) bgcolor(white);
	   graph export "${res}\fig6a.pdf", replace as(pdf);


* Panel B. Average redistribution vs. Multiplier Differences;
#delimit;
use "${data}\spectator.dta", replace;
drop if condition == "CF";
drop if condition == "EP";

gen shr_red_res =.;
reg shr_red pi if info == "no" ; predict xb; gen temp = shr_red - xb; replace shr_red_res = temp if info == "no"  ; qui sum shr_red if info == "no"  , meanonly; replace shr_red_res = shr_red_res + r(mean) if info == "no"  ; drop temp xb;
reg shr_red pi if info == "yes"; predict xb; gen temp = shr_red - xb; replace shr_red_res = temp if info == "yes" ; qui sum shr_red if info == "yes" , meanonly; replace shr_red_res = shr_red_res + r(mean) if info == "yes" ; drop temp xb;

xtile quant = diff_m , nq(15);
bys quant: egen xgrid = mean(diff_m);

lpoly shr_red     diff_m if info == "no"   , gen(temp) nograph degree(1) at(xgrid) bw(2) kernel(triangle); by quant: egen lpoly_noinfo     =mean(temp); drop temp;
lpoly shr_red     diff_m if info == "yes"  , gen(temp) nograph degree(1) at(xgrid) bw(2) kernel(triangle); by quant: egen lpoly_info       =mean(temp); drop temp;
lpoly shr_red_res diff_m if info == "no"   , gen(temp) nograph degree(1) at(xgrid) bw(2) kernel(triangle); by quant: egen lpoly_res_noinfo =mean(temp); drop temp;
lpoly shr_red_res diff_m if info == "yes"  , gen(temp) nograph degree(1) at(xgrid) bw(2) kernel(triangle); by quant: egen lpoly_res_info   =mean(temp); drop temp;

gen lpoly     = .; replace lpoly     = lpoly_noinfo     if info == "no"  ; replace lpoly     = lpoly_info       if info == "yes" ;
gen lpoly_res = .; replace lpoly_res = lpoly_res_noinfo if info == "no"  ; replace lpoly_res = lpoly_res_info   if info == "yes" ;

gen n = 1;

collapse (mean) shr_red* lpoly lpoly_res diff_m (sd) sd = shr_red (semean) semean = shr_red semean_res = shr_red_res (sum) n, by(info quant);

gen up = shr_red + 1.96*semean;
gen lo = shr_red - 1.96*semean;

gen up_res = shr_red_res + 1.96*semean_res;
gen lo_res = shr_red_res - 1.96*semean_res;


#delimit;
twoway scatter shr_red       diff_m if info == "no" , lcolor(black)      mcolor(black)   msym(O) 
 	|| rcap    up lo         diff_m if info == "no" , lcolor(gs8)   lpattern(dash) lwidth(vthin)
    || line    lpoly         diff_m if info == "no" , lcolor(black)
	   xtitle("Difference (multiplier winner) minus (multiplier loser)") xlabel(-.5(.5)3 , format(%4.1fc)) xscale(titlegap(2))
	   ytitle("Average fraction of earnings redistributed")              ylabel(0(.1).5  , format(%4.2fc)  angle(0) gmin gmax)
	   legend(off)  
	   graphregion(color(white)) bgcolor(white);
	   graph export "${res}\fig2b.pdf", replace as(pdf);  

#delimit;
twoway scatter shr_red       diff_m if info == "no"  , lcolor(black)      mcolor(black) msym(O) 
    || scatter shr_red       diff_m if info == "yes" , lcolor(gs5)        mcolor(gs5)   msym(T) 
    || scatter shr_red_res   diff_m if info == "yes" , lcolor(gs10)       mcolor(gs10)  msym(S) 
 	|| rcap    up     lo     diff_m if info == "no"  , lcolor(black%75)   lpattern(dash) lwidth(vthin)
 	|| rcap    up     lo     diff_m if info == "yes" , lcolor(gs5%75)     lpattern(dash) lwidth(vthin)
 	|| rcap    up_res lo_res diff_m if info == "yes" , lcolor(gs10%75)    lpattern(dash) lwidth(vthin)
    || line    lpoly         diff_m if info == "no"  , lcolor(black)
    || line    lpoly         diff_m if info == "yes" , lcolor(gs5)
    || line    lpoly_res     diff_m if info == "yes" , lcolor(gs10)
	   xtitle("Difference (multiplier winner) minus (multiplier loser)") xlabel(-.5(.5)3 , format(%4.1fc)) xscale(titlegap(2))
	   ytitle("Average fraction of earnings redistributed")              ylabel(0(.1).5   , format(%4.2fc)  angle(0) gmin gmax)
       legend(order(1 "Baseline Lucky Opportunities" 2 "Information Treatment" 3 "Information + Controlling for {&pi}") col(1) ring(0) position(5) bmargin(medium))
	   graphregion(color(white)) bgcolor(white);
	   graph export "${res}\fig6b.pdf", replace as(pdf);	  
	  
	  
* Figure 3 - Relationship between the probability that the best performer lost and features of the luck environment observed by spectators;

#delimit;
use "${data}/spectator.dta", replace;
drop if condition == "EP";
drop if info      == "yes";
gen one_m_pi = 1 - pi/100;
drop if diff_m<0;
replace q  = q/100;

* Panel A;
#delimit;
twoway lfit one_m_pi q, lwidth(0.75) color(black) msize(large)
    || scatter one_m_pi q, mcolor(gs10%20) 
       xlabel(0(.2)1   , format(%4.2fc)) 
	   ylabel(0(.1).5  , format(%4.2fc)   angle(0) gmin gmax)
       xtitle("Probability that a coin flip determined the winner")   xscale(titlegap(2)) 
	   ytitle("Probability that the best performer lost (1 - {&pi})") yscale(titlegap(2)) 
	   graphregion(color(white)) bgcolor(white) plotregion(margin(zero)) legend(off);	   
  	   graph export "${res}/fig3a.pdf", replace as(pdf);

#delimit;
twoway scatter one_m_pi diff_m, mcolor(gs10%20)
   ||  lowess  one_m_pi diff_m, lwidth(0.75) lcolor(black)
	   xtitle("Difference (multiplier winner) - (multiplier loser)")            xlabel(0(.5)3 , format(%4.1fc)                    ) xscale(titlegap(2)) 
	   ytitle("Probability that the best performer lost (1 - {&pi})") ylabel(0(.1).5, format(%4.2fc)  angle(0) gmin gmax)
	   graphregion(color(white)) bgcolor(white) legend(off);
  	   graph export "${res}/fig3b.pdf", replace as(pdf);	   



* Figure 4 - The effect of providing information about $\pi$ 
#delimit;
use "${data}/spectator.dta", replace;
drop if condition == "EP";

local my_green  "0 158 115%75";
local my_green2 "0 111 80%75" ;
local my_blue   "0 114 178%75"; 
local my_blue2  "0 80 125%75" ; 

gen n = 1;
collapse (mean) shr_red (sum) n (min) min_pi=pi (max) max_pi=pi (semean) semean = shr_red, by(condition pi_bin info); gen mid_bin = (max_pi+min_pi)/2;

replace mid_bin = 100 - mid_bin;
replace mid_bin = mid_bin+0.35 if condition == "EA";
replace mid_bin = mid_bin-0.35 if condition == "CF";


#delimit;
cap drop x* y*; gen x1 = 25; gen x2 = 25; gen y1 = .45; gen y2 = .49;
cap drop w* z*; gen w1 = 25; gen w2 = 25; gen z1 = .05; gen z2 = .01;

cap drop a* b*; gen a1 = 4 ; gen a2 = 1 ; gen b1 = .45  ; gen b2 = .45;
cap drop c* d*; gen c1 = 45; gen c2 = 49; gen d1 = .045 ; gen d2 = .045;

#delimit;
gen up = shr_red + 1.96*semean;
gen lo = shr_red - 1.96*semean;

#delimit;
local emdash = ustrunescape("\u2013");
twoway connected shr_red mid_bin if condition == "CF" & info == "no",  lcolor("black")     lp(solid) mcolor("black")     msize(small) msym(D)
	|| connected shr_red mid_bin if condition == "EA" & info == "no",  lcolor("gray60")    lp(solid) mcolor("gray60")    msize(small) msym(O)
	|| connected shr_red mid_bin if condition == "CF" & info == "yes", lcolor("black")     lp(dash)  mcolor("black")     msize(small) msym(D)
	|| connected shr_red mid_bin if condition == "EA" & info == "yes", lcolor("gray60")    lp(dash)  mcolor("gray60")    msize(small) msym(O)
	|| pcarrow y1 x1 y2 x2, lcolor(gs12)   mcolor(gs12)   
    || pcarrow z1 w1 z2 w2, lcolor(gs12)   mcolor(gs12)   
	|| pcarrow b1 a1 b2 a2, color(gs8) 
    || pcarrow d1 c1 d2 c2, color(gs8)
	   xtitle("Probability that the best performer lost (1 `emdash' {&pi})") xlabel(0(10)50, format(%4.0fc)) xscale(titlegap(2)) 
	   ytitle("Fraction of winner's earnings redistributed") ylabel(0(.1).5, format(%4.2fc)  angle(0) gmin gmax)
       yline(0.5, lpattern(dash) lcolor(gs12))      text(.43 25 "Earnings equalization", color(gs8))
       yline(0.0, lpattern(dash) lcolor(gs12))      text(.07 25 "Laissez-faire", color(gs8))
       xline(50,  lpattern(dash) lcolor(gs8)) text(.45 9 "Performance" "determines" "winner", color(gs8))
       xline(0, lpattern(dash) lcolor(gs8))   text(.05 40 "Chance" "determines" "winner", color(gs8))
       legend(order(1 "Lucky Outcomes" 2 "Lucky Opportunities" 3 "Lucky Outcomes (Info)" 4 "Lucky Opportunities (Info)" ) row(2))
	   graphregion(color(white)) bgcolor(white);
  	   graph export "${res}/fig4.pdf", replace;	   
  
 
* Figure 5 - Redistribution gap by $1-\pi$ in the baseline and information treatments;

#delimit;
use "${data}/spectator.dta", replace;
drop if condition == "EP";
gen n = 1;

collapse (mean) shr_red (sum) n (min) min_pi=pi (max) max_pi=pi (semean) se = shr_red, by(condition info pi_bin); gen mid_bin = (max_pi+min_pi)/2;

replace mid_bin = 100 - mid_bin;
drop pi_bin max_pi min_pi;

replace condition = "_" + lower(condition);

reshape wide shr_red se n, i(mid_bin info) j(condition) string;

gen    gap_red = shr_red_cf-shr_red_ea;
gen se_gap_red = (se_cf^2+se_ea^2)^(1/2);

#delimit;
gen up = gap_red + 1.96*se_gap_red;
gen lo = gap_red - 1.96*se_gap_red;
gen xaxis = mid_bin;

#delimit;
cap drop x* y*; gen x1 = 25; gen x2 = 25; gen y1 = .15; gen y2 = .19;
cap drop w* z*; gen w1 = 25; gen w2 = 25; gen z1 = .05; gen z2 = .01;

cap drop a* b*; gen a1 = 4 ; gen a2 = 1 ; gen b1 = .15  ; gen b2 = .15;
cap drop c* d*; gen c1 = 45; gen c2 = 49; gen d1 = .045 ; gen d2 = .045;


replace xaxis = mid_bin + 0.5 if info == "yes";
local emdash = ustrunescape("\u2013");  
	  
#delimit;
twoway connect gap_red xaxis if info == "no"  , lcolor(black)     mcolor(black)     msize(small) msym(D)
	|| connect gap_red xaxis if info == "yes" , lcolor(gray)      mcolor(gray)      msize(small) msym(O)
	|| rcap      up lo xaxis if info == "no"  , lcolor(black)     lpattern(dash)    lwidth(vthin)
	|| rcap      up lo xaxis if info == "yes" , lcolor(gray)      lpattern(dash)    lwidth(vthin)
	   xtitle("Probability that the best performer lost (1 `emdash' {&pi})") xlabel(0(10)50, format(%4.0fc)) xscale(titlegap(2)) 
	   ytitle("Redistribution Gap (percentage points)")                     ylabel(-.15(.05).15, format(%4.2fc)  angle(0) gmin gmax)
	   yline(0 , lcolor(black)) xsize(7) ysize(5)
       legend(order(1 "Baseline" 2 "With {&pi} Information") row(2) ring(0) pos(5) )
	   graphregion(color(white)) bgcolor(white);
  	   graph export "${res}/fig5.pdf", replace;
	   
	  
* Figure 7: Redistribution by 1 - pi in the baseline and ex-post lucky opportunities conditions;

#delimit;
use "${data}/spectator.dta", replace;
local my_green   "0 158 115";
local my_blue    "0 114 178"; 
local my_purple  "128 0 128"; 

drop if info      == "yes";
gen n = 1;
collapse (mean) shr_red (sum) n (min) min_pi=pi (max) max_pi=pi (semean) semean = shr_red, by(condition pi_bin); gen mid_bin = (max_pi+min_pi)/2;

replace mid_bin = 100 - mid_bin;
replace mid_bin = mid_bin+0.35 if condition == "EA";
replace mid_bin = mid_bin-0.35 if condition == "EP";


#delimit;
cap drop x* y*; gen x1 = 25; gen x2 = 25; gen y1 = .45; gen y2 = .49;
cap drop w* z*; gen w1 = 25; gen w2 = 25; gen z1 = .05; gen z2 = .01;

cap drop a* b*; gen a1 = 4 ; gen a2 = 1 ; gen b1 = .45  ; gen b2 = .45;
cap drop c* d*; gen c1 = 45; gen c2 = 49; gen d1 = .045 ; gen d2 = .045;

#delimit;
gen up = shr_red + 1.96*semean;
gen lo = shr_red - 1.96*semean;

	
#delimit;
local emdash = ustrunescape("\u2013");
twoway connected shr_red mid_bin if condition == "EA", lcolor("black")     lp(solid)      mcolor("black")    msize(small) msym(O)
	|| connected shr_red mid_bin if condition == "EP", lcolor("gray")      lp(solid)      mcolor("gray")     msize(small) msym(D)
	|| rcap      up lo   mid_bin if condition == "EA", lcolor("black")     lpattern(dash) lwidth(vthin)
	|| rcap      up lo   mid_bin if condition == "EP", lcolor("gray")      lpattern(dash) lwidth(vthin)
	|| pcarrow y1 x1 y2 x2, lcolor(gs12)   mcolor(gs12)   
    || pcarrow z1 w1 z2 w2, lcolor(gs12)   mcolor(gs12)   
	|| pcarrow b1 a1 b2 a2, color(gs8) 
    || pcarrow d1 c1 d2 c2, color(gs8)
	   xtitle("Probability that the best performer lost (1 `emdash' {&pi})") xlabel(0(10)50, format(%4.0fc)) xscale(titlegap(2)) 
	   ytitle("Fraction of winner's earnings redistributed") ylabel(0(.1).5, format(%4.2fc)  angle(0) gmin gmax)
       yline(0.5, lpattern(dash) lcolor(gs12))   text(.43 25 "Earnings equalization", color(gs8))
       yline(0.0, lpattern(dash) lcolor(gs12))   text(.07 25 "Laissez-faire", color(gs8))
       xline(50,  lpattern(dash) lcolor(gs8)) text(.45 9 "Performance" "determines" "winner", color(gs8))
       xline(0, lpattern(dash) lcolor(gs8))   text(.05 40 "Chance" "determines" "winner", color(gs8))
       legend(order(1 "Lucky Opportunities" 2 "Ex-Post Lucky Opportunities") row(1))
	   graphregion(color(white)) bgcolor(white);
  	   graph export "${res}/fig7.pdf", replace;
	   